package com.inei.eea2014.model.connection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import com.inei.eea2014.R;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class UtilDB extends SQLiteOpenHelper {

	private Context ctx;
	public static final String DATABASE = "eea2014.db";
	public static final int VERSION = 1;

	public UtilDB(Context ctx) {
		super(ctx, DATABASE, null, VERSION);
		this.ctx = ctx;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Document doc = null;
		NodeList stm = null;
		try {
			DocumentBuilder dbuilder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();

			// Marco
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqlmarco));
			stm = doc.getElementsByTagName("Marco");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Caratula
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqlcaratula));
			stm = doc.getElementsByTagName("Caratula");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Capitulo1
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqlcapitulo1));
			stm = doc.getElementsByTagName("Capitulo1");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Capitulo2
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqlcapitulo2));
			stm = doc.getElementsByTagName("Capitulo2");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Capitulo3
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqlcapitulo3));
			stm = doc.getElementsByTagName("Capitulo3");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Capitulo4
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqlcapitulo4));
			stm = doc.getElementsByTagName("Capitulo4");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// TipoVia
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqltipovia));
			stm = doc.getElementsByTagName("TipoVia");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Dep
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqldep));
			stm = doc.getElementsByTagName("Dep");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Pro
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqlpro));
			stm = doc.getElementsByTagName("Pro");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Dis
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqldis));
			stm = doc.getElementsByTagName("Dis");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Ciiu
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqlciiu));
			stm = doc.getElementsByTagName("Ciiu");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Clanae
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqlclanae));
			stm = doc.getElementsByTagName("Clanae");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// TipoOrganizacion
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqltipoorganizacion));
			stm = doc.getElementsByTagName("TipoOrganizacion");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Entrevista
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqlentrevista));
			stm = doc.getElementsByTagName("Entrevista");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Resultado
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqlresultado));
			stm = doc.getElementsByTagName("Resultado");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

			// Justificacion
			doc = dbuilder.parse(ctx.getResources().openRawResource(
					R.raw.sqljustificacion));
			stm = doc.getElementsByTagName("Justificacion");
			for (int i = 0; i < stm.getLength(); i++) {
				db.execSQL(stm.item(i).getChildNodes().item(0).getNodeValue());
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

}
